/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TUILayer;

import DomainLayer.*;
import java.util.Scanner;
import utils.*;

/**
 *
 * @author Middreks
 * Licensed under GNU/GPLv2
 * Credits go to Group 4
 */
public class LoanUI
{

    private Tracking tracking;
    private AddressBook addressBook;
    private int selection = -1;
    private Scanner input;
    private String string = "";
    private IdGenerator loanId;

    public LoanUI(Tracking tracking, AddressBook addressBook)
    {
        input = new Scanner(System.in);
        this.tracking = tracking;
        this.addressBook = addressBook;
        loanId = new IdGenerator();
    }

    /**
     * Menu of the Loan Management
     */
    public void loanMenu()
    {
        while (selection != 0)
        {
            System.out.printf("Please choose from the following\n"
                    + "\t*1* Loan a copy\n"
                    + "\t*2* Return a copy\n"
                    + "\t*3* List loans\n"
                    + "\n"
                    + "\t*0* Exit to Main Menu\n");

            if (input.hasNextInt())
            {
                selection = input.nextInt();
                switch (selection)
                {
                    case 0:
                        break;
                    case 1:
                        ConsoleUtils.clear();
                        if (tracking.getAvailableDVDs().length == 0 )
                            System.out.println("There no available dvds.");
                        else if (addressBook.getPeople().length == 0)
                            System.out.println("There no people in the address"
                                    + "book");
                        else
                            loanCopy();
                        break;
                    case 2:
                        ConsoleUtils.clear();
                        if (tracking.getLoanedDvds().length == 0)
                            System.out.println("All dvds have been returned.");
                        else
                            returnCopy();
                        break;
                    case 3:
                        ConsoleUtils.clear();
                        if (tracking.getLoans().length == 0)
                            System.out.println("No DVDs have been loaned.");
                        else
                            Printer.print(tracking.getLoans());
                        break;
                    default:
                        System.out.println(" Incorrect input!");
                }
            } else
            {
                string = input.next();
                ConsoleUtils.clear();
                System.out.printf("%s is not a number!\n", string);
            }

        }
        selection = -1;
        ConsoleUtils.clear();
    }

    private void loanCopy()
    {
        DVD[] dvds = null;
        Person[] people = addressBook.getPeople();
        Person renter = null;
        DVD dvdToBeRented = null;
        String borrowDate = "n/a";
        String period = "n/a";
        String state = "n/a";
        Loan loan = null;

        while (selection != 0)
        {
            
            if (tracking.getAvailableDVDs().length == 0 )
            {
                System.out.println("There are no more available DVDs.");
                selection = -1;
                return;
            }

            dvds = tracking.getAvailableDVDs();
            do
            {
                ConsoleUtils.clear();
                Printer.print(dvds);
                System.out.print("Enter the number of the DVD you want to rent" +
                        " or 0 to go back: ");
                if (input.hasNextInt())
                {
                    selection = input.nextInt();
                    if (selection <= dvds.length && selection > 0)
                    {
                        dvdToBeRented = dvds[selection - 1];
                    }
                    else if (selection != 0)
                    {
                        ConsoleUtils.clear();
                        System.out.println("Incorrect input!\n");
                    }
                }
                else
                {
                    string = input.next();
                    ConsoleUtils.clear();
                    System.out.printf("%s is not a number!\n", string);
                }
            } while (dvdToBeRented == null && selection != 0);

            if (selection == 0)
            {
                ConsoleUtils.clear();
                selection = -1;
                return;
            }
            
            do
            {
                ConsoleUtils.clear();
                Printer.print(people);
                System.out.print("Enter the number of the person who wants to rent "
                        + "the DVD or 0 to go back: ");
                if (input.hasNextInt())
                {
                    selection = input.nextInt();
                    if (selection <= dvds.length && selection > 0)
                    {
                        renter = people[selection - 1];
                    }
                    else if (selection != 0)
                    {
                        ConsoleUtils.clear();
                        System.out.println("Incorrect input!\n");
                    }
                }
                else
                {
                    string = input.next();
                    ConsoleUtils.clear();
                    System.out.printf("%s is not a number!\n", string);
                }
            }
            while (renter == null && selection != 0);

            if (selection == 0)
            {
                ConsoleUtils.clear();
                selection = -1;
                return;
            }

            System.out.print("Enter borrow date: ");
            if (input.hasNext())
                borrowDate = input.next();
            System.out.print("Enter borrow period: ");
            if (input.hasNext())
                period = input.next();
            System.out.print("Enter the state of the borrowed item: ");
            if (input.hasNext())
                state = input.next();

            loan = new Loan(loanId.getNextId(), borrowDate, state, period,
                    renter, tracking.getCopiesOfDVD(dvdToBeRented)[0]);
            tracking.loanDVD(loan);
        }

        selection = -1;
        ConsoleUtils.clear();
    }

    private void returnCopy()
    {
        Loan[] loans = null;
        Person renter = null;
        Loan loan = null;
        Person[] renters;

        while (selection != 0)
        {
            if (tracking.getLoanedDvds().length == 0)
            {
                ConsoleUtils.clear();
                System.out.println("All the dvds have been returned.");
                selection = -1;
                return;
            }

            renters = tracking.getRenters();

            do
            {
                ConsoleUtils.clear();
                Printer.print(renters);
                System.out.print("Enter the number of the renter or 0 to go " +
                        "back: ");
                if (input.hasNextInt())
                {
                    selection = input.nextInt();
                    if (selection <= renters.length && selection > 0)
                    {
                        renter = renters[selection - 1];
                    }
                    else if (selection != 0)
                    {
                        ConsoleUtils.clear();
                        System.out.println("Incorrect input!\n");
                    }
                }
                else
                {
                    string = input.next();
                    ConsoleUtils.clear();
                    System.out.printf("%s is not a number!\n", string);
                }
            } while (renter == null && selection != 0);

            if (selection == 0)
            {
                ConsoleUtils.clear();
                selection = -1;
                return;
            }

            loans = tracking.getLoansByPerson(renter);

            do
            {
                ConsoleUtils.clear();
                Printer.print(loans);
                System.out.print("Enter the number of the loan that was " +
                        "returned or 0 to go back: ");
                if (input.hasNextInt())
                {
                    selection = input.nextInt();
                    if (selection <= loans.length && selection > 0)
                    {
                        loan = loans[selection - 1];
                    }
                    else if (selection != 0)
                    {
                        ConsoleUtils.clear();
                        System.out.println("Incorrect input!\n");
                    }
                }
                else
                {
                    string = input.next();
                    ConsoleUtils.clear();
                    System.out.printf("%s is not a number!\n", string);
                }
            }
            while (loan == null && selection != 0);

            if (selection == 0)
            {
                ConsoleUtils.clear();
                selection = -1;
                return;
            }

            tracking.returnDVD(loan);
        }

        selection = -1;
        ConsoleUtils.clear();
    }
}
